package com.jhws.common.base.api;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.common.base.bo.JsonRespData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jhws.business.comm.bo.TblBuildingDimBo;
import com.jhws.business.owner.bo.TblOwnerDimBo;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.util.net.JhHttpClient;
import com.jhws.business.comm.service.ITblBuildingDimService;
import com.jhws.business.owner.service.ITblOwnerDimService;
import com.jhws.sys.user.service.ITblUserDimService;


/**
 * @Create : 2016-9-26 下午5:45:41
 * @author : Alex Tan
 * @Comment : 本地局域网服务器使用的控制器， 本地服务器的http client请求同步更新一些数据到本地
 */

@Controller
@RequestMapping("/LocalSrv")
public class LocalSrvCtrl {
	
	final private String fileName = "vistorsToday.zip";

	@Resource
	ITblOwnerDimService ITblOwnerDimService;
	@Resource
	ITblUserDimService ITblUserDimService;
	@Resource
	ITblBuildingDimService IBuildingService;
	@Resource
	private OSSClientService ossClientService;

	/**
	 * @Comment: 同步访客信息图片到本地
	 * @Param: commCode - 小区编号
	 */
	@RequestMapping("downLoadVisitors")
	public void getOssPic(String commCode,String date,HttpServletRequest request,HttpServletResponse response) throws IOException {
		
		/* 设置response格式 */
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);
        
        /* 获取到今天的OSS中所有图片的路径列表 */
		List<String> list = ossClientService.getOssObjectList(commCode + "/outdoor/" + date +"/");
		
		OutputStream os = response.getOutputStream();
		ZipOutputStream zipOut = new ZipOutputStream(os);
		
		/* 循环下载 ,按照文件名路径压缩到zip文件中返回 */
		for(String path : list){
			int len=0;
			String url = ossClientService.getVisitorUrl(path);
			InputStream is = JhHttpClient.downloadFileInMem(url);
			zipOut.putNextEntry(new ZipEntry(path));
			byte[] buffer = new byte[1024 * 20];
			while (-1 != (len = is.read(buffer))) {
				zipOut.write(buffer, 0, len);
			}
			is.close();
		}
		
		os.flush();
		zipOut.close();
	}
	
	/**
	 * @Comment: 同步业主信息，通过backuped字段来控制是否需要同步出去
	 * @Param: commCode - 小区编号
	 */
	@RequestMapping("updateOwner")
	@ResponseBody
	public JsonRespData updateOwner(String commCode, String timeStamp) throws Exception{
		HashMap<String ,Object> map = new HashMap<String,Object>();
		if(timeStamp == null) {
			new JsonRespData("updateOwner",-1,"无timeStamp参数");
		}
		long lTimeStamp = Long.valueOf(timeStamp);
		List<TblOwnerDimBo> list = ITblOwnerDimService.findTblOwnerDimByWhere("where 1=1 and comm_code = '"+commCode+"' and lastModifyTimeStamp > "+lTimeStamp+"");
		for( TblOwnerDimBo bo : list){
			if(bo.getLastModifyTimeStamp() > lTimeStamp)
				lTimeStamp = bo.getLastModifyTimeStamp();
		}
		map.put("list", list);
		map.put("timeStamp", lTimeStamp);
		return new JsonRespData("updateOwner",1,map);
	}
	
	/**
	 * @Comment: 同步物业账号信息，假如物业修改过密码，就可以自动同步，由于数据量少，不设同步字段了
	 * @Param: commCode - 小区编号
	 */
	@RequestMapping("updateManagerAccount")
	@ResponseBody
	public JsonRespData updateManager(String commCode,HttpServletRequest request,HttpServletResponse response) throws Exception {
		List<TblUserDimBo> list = ITblUserDimService.findTblUserDimByWhere("where 1=1 and comm_code = '"+commCode+"'");
		return new JsonRespData("updateManagerAccount",1,list);
	}
	
	/* 
	 * @author Alex Tan
	 * @date   2017年1月9日
	 * @commnet  同步局域网服务器小区的楼栋信息
	 */
	@RequestMapping("updateBuildings")
	@ResponseBody
	public JsonRespData updateBuildings(String commCode, String timeStamp) throws Exception {
		long lTimeStamp = Long.valueOf(timeStamp);
		HashMap<String ,Object> map = new HashMap<String,Object>();
		List<TblBuildingDimBo> list = IBuildingService.findTblBuildingDimByWhere("where 1=1 and commCode = '"+commCode+"' and timeStamp > "+lTimeStamp+"");
		lTimeStamp = -1;
		for(TblBuildingDimBo bo:list){
			lTimeStamp = bo.getTimeStamp()>lTimeStamp?bo.getTimeStamp():lTimeStamp;
		}	
		map.put("list", list);
		map.put("timeStamp", lTimeStamp);
		return new JsonRespData("updateBuildings",1,map);
	}
}

